package com.ego.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ego.common.pojo.PageResult;
import com.ego.exception.enums.ExceptionEnum;
import com.ego.exception.exception.EgoException;
import com.ego.item.mapper.BrandMapper;
import com.ego.item.pojo.Brand;
import com.ego.item.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class BrandServiceImpl implements BrandService {

    @Resource
    private BrandMapper brandMapper;

    @Override
    @Transactional(readOnly = true)
    public PageResult page(Integer pageNo, Integer pageSize, String sortBy, Boolean descending, String key) {
        PageResult pageResult = null;
        try {
            QueryWrapper<Brand> wrapper = new QueryWrapper<>();
            wrapper.like("name",key).or().eq("letter",key.toUpperCase())//查询条件
                    .orderBy(StringUtils.isNotEmpty(sortBy),descending,sortBy);//排序
            Page<Brand> page = brandMapper.selectPage(new Page<>(pageNo, pageSize), wrapper);
            pageResult = new PageResult<>(page.getTotal(),page.getRecords());
        }catch (Exception e){
            EgoException.error(log, ExceptionEnum.BRAND_NOT_FOUND,e);
        }
        return pageResult;
    }

    @Override
    @Transactional
    public void save(Brand brand, List<Long> cids) {
        try {
            //新增品牌信息
            brandMapper.insert(brand);
            //中间表
            for (Long cid : cids) {
                brandMapper.insertCategoryBrand(cid,brand.getId());
            }
        }catch (Exception e){
            EgoException.error(log,ExceptionEnum.BRAND_SAVE_FAILED,e);
        }
    }

    @Override
    @Transactional
    public void update(Brand brand, List<Long> cids) {
        try {
            brandMapper.updateById(brand);
            brandMapper.deleteCategoryBrand(brand.getId());
            for (Long cid : cids) {
                brandMapper.insertCategoryBrand(cid,brand.getId());
            }
        }catch (Exception e){
            EgoException.error(log,ExceptionEnum.UPDATE_BRAND_FAILED,e);
        }
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        try {
            brandMapper.deleteCategoryBrand(id);
            brandMapper.deleteById(id);
        }catch (Exception e){
            EgoException.error(log,ExceptionEnum.DELETE_BRAND_EXCEPTION,e);
        }

    }

    @Override
    @Transactional(readOnly = true)
    public List<Brand> findById(Integer cid) {
        return brandMapper.selectByCid(cid);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Brand> findListByIds(List<Long> bids) {
        return brandMapper.selectBatchIds(bids);
    }
}
